package service.impl;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import security.MySecurityContextHandle;
import service.interf.IOperationLog;

import common.impl.exception.ServiceException;

import dao.interf.IIfwSrouterMapDAO;
import dao.interf.IOperationLogDAO;
import dao.pojo.IfwSrouterMap;
import dao.pojo.OperationLog;

public class OperationLogImpl extends BaseDataServiceImpl implements
		IOperationLog {

	private IOperationLogDAO operationLogDao;
	private IIfwSrouterMapDAO ifwSrouterMapDao;

	public void setOperationLogDao(IOperationLogDAO operationLogDao) {
		this.operationLogDao = operationLogDao;
	}

	public void setIfwSrouterMapDao(IIfwSrouterMapDAO ifwSrouterMapDao) {
		this.ifwSrouterMapDao = ifwSrouterMapDao;
	}

	public String getList(int page, int rows) throws Exception {
		// 验证
		String userName = MySecurityContextHandle.getUserName();
		if (!"admin".equals(userName))
			throw new ServiceException("请先登录");
		int start = (page - 1) * rows;
		if (start < 0)
			start = 0;
		long total = operationLogDao.getAllCount();
		JSONObject jo = new JSONObject();
		jo.put("total", total);
		List<OperationLog> list = operationLogDao.findAll(start, rows,
				" order by model.id desc");
		JSONArray ja = new JSONArray();
		if (null != list && !list.isEmpty()) {
			List<IfwSrouterMap> map;
			for (OperationLog ol : list) {
				JSONObject jobj = new JSONObject();
				jobj.put("id", ol.getId());
				jobj.put("createTime", ol.getCreateTime());
				Integer type = ol.getType();
				String dsn = ol.getDsn();
				if (type.intValue() != 1 && !StringUtils.isEmpty(dsn)) {
					map = ifwSrouterMapDao.findByPropertiesRule(
							new String[] { "sn" }, new Object[] { dsn }, null,
							null, 0, 1);
					if (null != map && !map.isEmpty())
						dsn = map.get(0).getFwsn();
				}
				jobj.put("dsn", dsn);
				jobj.put("model", ol.getModel());
				jobj.put("newTime", ol.getNewTime());
				jobj.put("otherTime", ol.getOtherTime());
				String ssn = ol.getSsn();
				if (type.intValue() != 1 && !StringUtils.isEmpty(ssn)) {
					map = ifwSrouterMapDao.findByPropertiesRule(
							new String[] { "sn" }, new Object[] { ssn }, null,
							null, 0, 1);
					if (null != map && !map.isEmpty())
						ssn = map.get(0).getFwsn();
				}
				jobj.put("ssn", ssn);
				jobj.put("supplier", ol.getSupplier());
				jobj.put("type", type);
				ja.add(jobj);
			}
		}
		jo.put("rows", ja);
		return jo.toString();
	}

	public String add(Integer type, String dsn, String ssn, String newTime,
			String otherTime, String model, String supplier) throws Exception {
		OperationLog ol = new OperationLog();
		ol.setCreateTime(myDateHandle.MyStringToISODateTimeString(myDateHandle
				.getCurrentDTString()));
		ol.setDsn(dsn);
		ol.setSsn(ssn);
		ol.setModel(model);
		ol.setNewTime(newTime);
		ol.setOtherTime(otherTime);
		ol.setSupplier(supplier);
		ol.setType(type);
		operationLogDao.save(ol);
		return "1";
	}

}
